package com.wk.hooker;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.wk.hooker.util.RefreshLoginActivity;

import java.util.List;

/**
 * Created by wuchu on 16/5/19.
 */
public class CleaMsgBroadcast extends BroadcastReceiver {



    public final static  String Clear_msg="clear_msg";

    public final static  String  Time ="time";

    public final static  String  fileName="filename";

    @Override
    public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
        if("org.wkhooker.action.setalarm".equals(intent.getAction())){
            //receive the brocast that we defined
            Toast.makeText(context, "接收到闹钟广播", Toast.LENGTH_SHORT).show();
            SharedPreferences pref = context.getSharedPreferences("weixinok", Context.MODE_WORLD_READABLE);
            SharedPreferences.Editor editor =pref.edit();
            editor.putInt(Clear_msg,1);
            editor.commit();

            if (!isRunningApp(context.getApplicationContext(), "com.tencent.mm")) {
                Intent intent2 = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                context.startActivity(intent2);
                context.startActivity(intent2);
            } else {
                Intent intent3 = new Intent(context, RefreshLoginActivity.class);
                intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent3);
            }
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    SharedPreferences pref = context.getSharedPreferences("weixinok", Context.MODE_WORLD_READABLE);
                    SharedPreferences.Editor editor =pref.edit();
                    editor.putInt(Clear_msg,0);
                    editor.commit();
                }
            },2000);
        }else if("org.wkhooker.activity.show".equals(intent.getAction())){
            Intent intent3 = new Intent(context, MainActivity.class);
            intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent3);
        }
    }

    public boolean isRunningApp(Context context, String packageName) {
        boolean isAppRunning = false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        for (ActivityManager.RunningTaskInfo info : list) {

            String s = info.baseActivity.getClassName();
            if (info.topActivity.getPackageName().equals(packageName) && (s.contains("LauncherUI"))) {
                isAppRunning = true;
                Log.v("WC", "已经在运行");
                break;
            } else if (info.topActivity.getPackageName().equals(packageName)) {
                break;
            }
        }

        return isAppRunning;
    }
}
